import {
  Folder,
  FolderOpened,
  Document,
  Collection,
  Tickets,
  Picture,
  VideoCamera,
  Headset
} from '@element-plus/icons-vue'

// 格式化文件大小
export const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 Bytes'
  const k = 1024
  const sizes = ['Bytes', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 获取文件图标
export const getFileIcon = (file) => {
  const iconMap = {
    folder: Folder,
    word: Document,
    excel: Collection,
    ppt: Tickets,
    pdf: Document,
    image: Picture,
    video: VideoCamera,
    audio: Headset,
    text: Document
  }
  
  if (file.type === 'folder') return Folder
  return iconMap[file.fileType] || Document
}

// 获取文件扩展名
export const getFileExtension = (file) => {
  return file.label.split('.').pop().toLowerCase()
}

// 获取预览类型
export const getPreviewType = (file) => {
  const ext = getFileExtension(file)
  const typeMap = {
    jpg: 'image', jpeg: 'image', png: 'image', gif: 'image', webp: 'image',
    pdf: 'pdf',
    doc: 'office', docx: 'office', xls: 'office', xlsx: 'office', ppt: 'office', pptx: 'office',
    txt: 'text', json: 'text', js: 'text', css: 'text', html: 'text', xml: 'text', md: 'text',
    mp4: 'video', avi: 'video', mov: 'video', wmv: 'video', webm: 'video',
    mp3: 'audio', wav: 'audio', aac: 'audio', ogg: 'audio'
  }
  return typeMap[ext] || 'unsupported'
}

// 递归删除节点
export const removeNode = (nodes, id) => {
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i].id === id) {
      nodes.splice(i, 1)
      return true
    }
    if (nodes[i].children && removeNode(nodes[i].children, id)) {
      return true
    }
  }
  return false
}

// 查找路径
export const findPath = (nodes, targetId) => {
  const path = []
  const find = (nodes, targetId) => {
    for (const node of nodes) {
      if (node.id === targetId) {
        path.unshift(node)
        return true
      }
      if (node.children && find(node.children, targetId)) {
        path.unshift(node)
        return true
      }
    }
    return false
  }
  find(nodes, targetId)
  return path
}

// 获取文件类型分类
export const getFileType = (file) => {
  const type = file.type
  const ext = file.name.split('.').pop().toLowerCase()
  
  if (type.includes('word') || ext === 'doc' || ext === 'docx') {
    return 'word'
  } else if (type.includes('excel') || ext === 'xls' || ext === 'xlsx') {
    return 'excel'
  } else if (type.includes('powerpoint') || ext === 'ppt' || ext === 'pptx') {
    return 'ppt'
  } else if (type.includes('pdf') || ext === 'pdf') {
    return 'pdf'
  } else if (type.includes('image')) {
    return 'image'
  } else if (type.includes('video')) {
    return 'video'
  } else if (type.includes('audio')) {
    return 'audio'
  } else if (type.includes('text') || ['txt', 'md'].includes(ext)) {
    return 'text'
  } else {
    return 'other'
  }
}